package com.pine.sunshineelder.bed.web.handler;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pine.sunshineelder.bed.ao.ElderBedAo;
import com.pine.sunshineelder.bed.ao.ElderBedModAo;
import com.pine.sunshineelder.bed.service.ElderBedService;
import com.pine.sunshineelder.bed.vo.ElderBedVo;
import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dto.JSONResponse;
import com.pine.sunshineelder.common.dto.QueryResult;




@RestController
@RequestMapping("/api/elderbed")
public class ElderBedHandler implements StringHelper{
	@Autowired
	ElderBedService service;
	
	@PostMapping("/add")
	public JSONResponse registerElderBed(ElderBedAo ao) throws Exception {
		ElderBedVo elderBed = service.addElderBed(ao);

		return JSONResponse.ok(elderBed);
	}
	
	@GetMapping("/{id}")
	public JSONResponse selectByElderBedId(@PathVariable("id") String id) throws Exception {
		ElderBedVo elderBedId = service.selectById(id);
		return JSONResponse.ok(elderBedId);
	}
	
	@DeleteMapping("/del/{id}")
	public JSONResponse deleteElderBed(@PathVariable("id") String id) throws Exception{
		service.deleteElderBedById(id);
		return JSONResponse.ok();
	}
	
	@GetMapping("/list")
	public JSONResponse listElderBed(String key, String pageNo, String pageSize) throws Exception {
		System.out.println("--------->");
		Integer pageNoUse = parseInt(pageNo, null);
		Integer pageSizeUse = parseInt(pageSize, null);
		QueryResult result = service.queryByKey(key, pageNoUse, pageSizeUse);
		return JSONResponse.ok(result);
	}
	
	@PutMapping("/mod/{id}")
	public JSONResponse modifyElderBed(@PathVariable String id, ElderBedModAo ao) throws Exception {
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("elderId", ao.getElderId());
	    map.put("bedId", ao.getBedId());
	    map.put("createUserId", ao.getCreateUserId());
//	    map.put("createTime", ao.getCreateTime());
//	    map.put("endTime", ao.getEndTime());
	    map.put("leaveReason", ao.getLeaveReason());
		
		System.out.println(map);
		service.modifyElderBed(id, map);
		return JSONResponse.ok();
	}

}
